<?php
include 'base.php';
include 'requireLogin.php';
include 'connectdb.php';
?>
<?php
if (isset($_POST['ok_button'])){
    $varDate = $_POST['a_date'];
    $varDate1= $_POST['i_date'];
    $varSeller = $_POST['b_seller'];
    $varCustomer = $_POST['b_customer'];
    echo"<table border='1'>
        <tr>
        <th>Mã hóa đơn</th>
        <th>Mã sản phẩm</th>
        <th>Giá</th>
        <th>Số lượng</th>
        <th>Mã khách hàng</th>
        <th>Mã nhân viên</th>
        <th>Khuyến mãi</th>
        <th>Ngày</th>
        </tr>";
    if(($varSeller =="all")&&($varCustomer=="all"))
    {
        $stSQL="SELECT bill.* FROM bill Where bill.date <'" .$varDate. "' and bill.date >'" .$varDate1."' and bill.status='1'";
        $result=mysql_query($stSQL);
        $sum=0;
        while($row = mysql_fetch_array($result))
       {   
            $sum = $sum + $row['total'];
            echo "<tr>";
            echo"<td>".$row['invoice_code']. "</td>";
            echo"<td>".$row['product_code']. "</td>";
            echo"<td>".$row['price']."</td>";
            echo"<td>".$row['quantity']. "</td>";
            echo"<td>".$row['customer_code']. "</td>";
            echo"<td>".$row['seller_code']. "</td>";
            echo"<td>".$row['discount']. "</td>";
            echo"<td>".$row['date']. "</td>";
            echo "</tr>";
            echo"</table>";
        }
        echo"Tổng giá trị là : ";
        echo"".number_format($sum,3)." VND";
    }else if(($varSeller =="all")&&($varCustomer!="all")){
        $stSQL="SELECT bill.* FROM bill Where bill.date <'" .$varDate. "' and bill.date >'" .$varDate1."' and bill.customer_code='".$varCustomer."' and bill.status='1'";
        $result=mysql_query($stSQL);
        $sum=0;
        while($row = mysql_fetch_array($result))
       {   
            $sum = $sum + $row['total'];
            echo "<tr>";
            echo"<td>".$row['code']. "</td>";
            echo"<td>".$row['product_code']. "</td>";
            echo"<td>".$row['price']."</td>";
            echo"<td>".$row['quantity']. "</td>";
            echo"<td>".$row['customer_code']. "</td>";
            echo"<td>".$row['seller_code']. "</td>";
            echo"<td>".$row['discount']. "</td>";
            echo"<td>".$row['date']. "</td>";
            echo "</tr>";
            echo"</table>";
        }
        echo"Tổng giá trị là : ";
        echo"".number_format($sum,3)." VND";
    }else if(($varSeller !="all")&&($varCustomer=="all")){
        $stSQL="SELECT bill.* FROM bill Where bill.date <'" .$varDate. "' and bill.date >'" .$varDate1."' and bill.seller_code='".$varSeller."' and bill.status='1'";
        $result=mysql_query($stSQL);
        $sum=0;
        while($row = mysql_fetch_array($result))
       {   
            $sum = $sum + $row['total'];
            echo "<tr>";
            echo"<td>".$row['code']. "</td>";
            echo"<td>".$row['product_code']. "</td>";
            echo"<td>".$row['price']."</td>";
            echo"<td>".$row['quantity']. "</td>";
            echo"<td>".$row['customer_code']. "</td>";
            echo"<td>".$row['seller_code']. "</td>";
            echo"<td>".$row['discount']. "</td>";
            echo"<td>".$row['date']. "</td>";
            echo "</tr>";
            echo"</table>";
        }
        echo"Tổng giá trị là : ";
        echo"".number_format($sum,3)." VND";

    }else{
        $stSQL="SELECT bill.* FROM bill Where bill.date <'" .$varDate. "' and bill.date >'" .$varDate1."' and bill.seller_code = '".$varSeller."' and bill.customer_code = '".$varCustomer."' and bill.status='1'";
        $result=mysql_query($stSQL);
        $sum=0;
        while($row = mysql_fetch_array($result))
        {   
            $sum = $sum + $row['total'];
            echo "<tr>";
            echo"<td>".$row['code']. "</td>";
            echo"<td>".$row['product_code']. "</td>";
            echo"<td>".$row['price']."</td>";
            echo"<td>".$row['quantity']. "</td>";
            echo"<td>".$row['customer_code']. "</td>";
            echo"<td>".$row['seller_code']. "</td>";
            echo"<td>".$row['discount']. "</td>";
            echo"<td>".$row['date']. "</td>";
            echo "</tr>";
            echo"</table>";
        }
        echo"Tổng giá trị là : ";
        echo"".number_format($sum,3)." VND";
    }
}
?>
<form method="post" action="">
        <div class="form_settings">                    
            <p> <span>Thống kê từ </span><input type="text" name="i_date" id = "i_date"></p>
            <p> <span>Đến trước ngày </span><input type="text" name="a_date" id = "a_date" value ="<?php echo date("Y-m-d");?>"></p>
            <p>
            <span>Nhân viên</span><select name="b_seller" >
            <option value="all" selected="selected">Tất cả</option>
            <?php
            $stSQL="select code from seller ";
            $resultsub=mysql_query($stSQL);
            if(mysql_num_rows($resultsub)> 0)
            {  
                while($row = mysql_fetch_array($resultsub))
                {
            ?>          
                    <option value="<?php echo"".$row['code']."";?>"><?php echo"".$row['code']."";?></option>
                
            <?php
                }
            }
            ?>
            </select>
            </p>
            <p>
            <span>Khách hàng</span><select name="b_customer" >
            <option value="all" selected="selected">Tất cả</option>
            <?php
            $stSQL="select code from customer ";
            $resultsub=mysql_query($stSQL);
            if(mysql_num_rows($resultsub)> 0)
            {  
                while($row = mysql_fetch_array($resultsub))
                {
            ?>          
                     <option value="<?php echo"".$row['code']."";?>"><?php echo"".$row['code']."";?></option>
                
            <?php
                }
            }
            ?>
            </select>
            </p>
            <span>&nbsp;</span><input class="submit" type="submit" id = "submit" name="ok_button" value="OK" />
        </div>
</form>    
<?php include 'footer.php';?>
